package com.kellyfeng.spittr.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.kellyfeng.spittr.domain.Spitter;
import com.kellyfeng.spittr.domain.Spittle;
import com.kellyfeng.spittr.repository.*;
import com.kellyfeng.spittr.exception.*;

@Controller
@RequestMapping("/spittles")
public class SpittleController {
	private SpittleRepository spittleRepository;

	@Autowired
	public SpittleController(SpittleRepository spittleRepository) {
		this.spittleRepository = spittleRepository;
	}
	@RequestMapping(method = RequestMethod.GET)
	public String spittles() {
		return "spittles";
	}
//	@RequestMapping(method = RequestMethod.GET)
//	public String spittles(Model model) {	
//		model.addAttribute("index", 1);
//		model.addAttribute("spittlePage", spittleRepository.findAll());
//		return "spittles";
//	}
//	@RequestMapping(method = RequestMethod.GET)
//	public List<Spittle> spittles() {
//		return spittleRepository.findAll();
//	}
//	@RequestMapping(method = RequestMethod.GET)
//	public String spittles(@PageableDefault(value = 20, sort = "id", direction = Direction.DESC) 
//	Pageable pageable, Model model) {	
//		int index = pageable.getPageNumber();
//		model.addAttribute("index", index);
//		model.addAttribute("spittlePage", spittleRepository.findAll(pageable));
//		return "spittles";
//	}
//	@RequestMapping(method = RequestMethod.GET)
//	public Page<Spittle> spittles(@PageableDefault(value = 20, sort = "id", direction = Direction.DESC) 
//    Pageable pageable) {
//		return spittleRepository.findAll(pageable);
//	}

//	@RequestMapping(method = RequestMethod.GET)
//	public Page<Spittle> spittles(@RequestParam(value = "page", defaultValue = "0") Integer page,
//	        @RequestParam(value = "size", defaultValue = "15") Integer size) {
//		Sort sort = new Sort(Direction.DESC, "id");
//	    Pageable pageable = new PageRequest(page, size, sort);
//		return spittleRepository.findAll(pageable);
//	}
	
	@RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
	public String spittle(@PathVariable("spittleId") long spittleId, Model model) {
		Spittle spittle = spittleRepository.findOne(spittleId);
		if(spittle == null){
			throw new SpittleNotFoundException();
		}
		model.addAttribute(spittle);
		
		return "spittle";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String saveSpittle(SpittleForm form, Model model) throws Exception {
		spittleRepository
				.save(new Spittle(null, form.getMessage(), new Date(), form.getLongitude(), form.getLatitude()));
		return "redirect:/spittles";
	}

}
